/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.magri.modulo2.aula1.exercicio1;

import javax.swing.JOptionPane;

/**
 *
 * @author Magri
 */
public class JFJogoDaVelha extends javax.swing.JFrame {

    /**
     * Creates new form JFJogoDaVelha
     */
    public JFJogoDaVelha() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPPrincipal = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jlVez = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jlTotalX = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jlTotalO = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Jogo da Veia");

        jPPrincipal.setPreferredSize(new java.awt.Dimension(300, 300));
        jPPrincipal.setLayout(new java.awt.GridLayout(3, 3));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 65)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 2, 2, new java.awt.Color(0, 0, 0)));
        jLabel1.setName("1");
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelMouseClicked(evt);
            }
        });
        jPPrincipal.add(jLabel1);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 65)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 2, 2, 2, new java.awt.Color(0, 0, 0)));
        jLabel2.setName("2");
        jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelMouseClicked(evt);
            }
        });
        jPPrincipal.add(jLabel2);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 65)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 2, 2, 0, new java.awt.Color(0, 0, 0)));
        jLabel3.setName("3");
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelMouseClicked(evt);
            }
        });
        jPPrincipal.add(jLabel3);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 65)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 0, 2, 2, new java.awt.Color(0, 0, 0)));
        jLabel4.setName("4");
        jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelMouseClicked(evt);
            }
        });
        jPPrincipal.add(jLabel4);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 65)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 2, 2, new java.awt.Color(0, 0, 0)));
        jLabel5.setName("5");
        jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelMouseClicked(evt);
            }
        });
        jPPrincipal.add(jLabel5);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 65)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 2, 0, new java.awt.Color(0, 0, 0)));
        jLabel6.setName("6");
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelMouseClicked(evt);
            }
        });
        jPPrincipal.add(jLabel6);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 65)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 0, 0, 2, new java.awt.Color(0, 0, 0)));
        jLabel7.setName("7");
        jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelMouseClicked(evt);
            }
        });
        jPPrincipal.add(jLabel7);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 65)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 0, 2, new java.awt.Color(0, 0, 0)));
        jLabel8.setName("8");
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelMouseClicked(evt);
            }
        });
        jPPrincipal.add(jLabel8);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 65)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 0, 0, new java.awt.Color(0, 0, 0)));
        jLabel9.setName("9");
        jLabel9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelMouseClicked(evt);
            }
        });
        jPPrincipal.add(jLabel9);

        getContentPane().add(jPPrincipal, java.awt.BorderLayout.CENTER);

        jPanel1.setPreferredSize(new java.awt.Dimension(100, 331));

        jButton1.setText("Novo Jogo");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addContainerGap(323, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.EAST);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel10.setText("Vez do: ");
        jPanel2.add(jLabel10);

        jlVez.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jlVez.setText("X");
        jPanel2.add(jlVez);

        jLabel11.setPreferredSize(new java.awt.Dimension(50, 20));
        jPanel2.add(jLabel11);

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel12.setText("X: ");
        jPanel2.add(jLabel12);

        jlTotalX.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jlTotalX.setText("0");
        jPanel2.add(jlTotalX);

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel14.setText(" O: ");
        jPanel2.add(jLabel14);

        jlTotalO.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jlTotalO.setText("0");
        jPanel2.add(jlTotalO);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelMouseClicked
        // TODO add your handling code here:
        
        javax.swing.JLabel quem = (javax.swing.JLabel) evt.getSource();
        
        if (quem.getText().isEmpty()) {
            quem.setText(ves);
            if (verificaJogada()) {
                JOptionPane.showMessageDialog(this, ves + " GANHOU!!");
                
                if (ves.equals("X")) {
                    jlTotalX.setText((++totalX) + "");
                } else {
                    jlTotalO.setText((++totalO) + "");
                }
                
                novoJogo();
                return;
            }
            ves = ves.equals("X") ? "O" : "X" ;
            jlVez.setText(ves);
        
        } else {
            JOptionPane.showMessageDialog(this, "OPS!!");
        }
        
    }//GEN-LAST:event_jLabelMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        novoJogo();
    }//GEN-LAST:event_jButton1ActionPerformed

    private boolean verificaJogada() {
        
        int validos[][] = {{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
        
        for (int i = 0; i < validos.length; i++) {
            int[] is = validos[i];
            if (verificaLinha(is[0], is[1], is[2])){
                return true;
            }
        }
        
        return false;
    }
    
    private boolean verificaLinha(int p1, int p2, int p3) {
        String v1 = getValorLabel(p1);
        String v2 = getValorLabel(p2);
        String v3 = getValorLabel(p3);
        
        System.out.println("v1: " + " v2: " + v2 + " v3: " + v3);
        
        return (!v1.isEmpty()) && v1.equals(v2) && v2.equals(v3);
    }
    
    private String getValorLabel(int posi) {
        
        System.out.println("LABEL: " + ((javax.swing.JLabel)jPPrincipal.getComponent(posi-1)).getName());
        return ((javax.swing.JLabel)jPPrincipal.getComponent(posi-1)).getText();
        
    }
    
    private void novoJogo() {
        ves = "X";
        
        jlVez.setText(ves);
        
        int x = jPPrincipal.getComponentCount();
        for (int i = 0; i < x; i++) {
            Object object = jPPrincipal.getComponent(i);
            if (object instanceof javax.swing.JLabel) {
                System.out.println("comp: " + ((javax.swing.JLabel)object).getName() + " i: " + i);
                ((javax.swing.JLabel)object).setText("");
            }
        }

    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFJogoDaVelha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFJogoDaVelha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFJogoDaVelha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFJogoDaVelha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFJogoDaVelha().setVisible(true);
            }
        });
    }
    
    String ves = "X";
    int totalX = 0;
    int totalO = 0;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPPrincipal;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel jlTotalO;
    private javax.swing.JLabel jlTotalX;
    private javax.swing.JLabel jlVez;
    // End of variables declaration//GEN-END:variables
}
